package com.sky.service.Impl;

import com.jfinal.kit.Kv;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sky.controller.LoginController;
import io.jboot.aop.annotation.Bean;
import com.sky.service.UserService;
import com.sky.model.User;
import io.jboot.db.model.Column;
import io.jboot.db.model.Columns;
import io.jboot.service.JbootServiceBase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 注意: 有些写法是 UserServiceProvider , 但是都是一个意思
 *
 * */
@Bean
public class UserServiceImpl extends JbootServiceBase<User> implements UserService {

    com.jfinal.log.Log log = Log.getLog(UserServiceImpl.class);

    @Override
    public Page<User> paginate(int page, int size) {
        //父类的对象  protected M DAO = null;
        return super.DAO.paginate(page, size);
    }


    /**
     * 类似于mybatis 使用mapper.xml
     * @param loginName
     * @return
     */
    @Override
    public List<Record> findByLoginName(String loginName) {
        Kv kv = Kv.by("loginName", loginName);
        SqlPara sqlPara = Db.getSqlPara("user-transaction.findByLoginName", kv);
        //sqlPara 源码就两个参数	String sql;	List<Object> paraList;
        // 参数类似于一条 SQL语句 : select xxx where xxx=xxx
        final List<Record> recordSqlTemplate = Db.find(sqlPara);
        final List<Record> recordsSqlStr = Db.find("select * from user ");
        return recordSqlTemplate;
    }

    /**单表查询
     * */
    @Override
    public List<User> findByColumns(HashMap<String, String> map) {
        Columns columns=Columns.create(); //设计模式???
        columns.add("login_name", map.get("loginName"));
        columns.add("password", map.get("password"));
        columns.ge("file_path", map.get("filePath"));
        columns.between("balance", map.get("balanceBottom"), map.get("balanceTop"));
        columns.between("create_date", map.get("createDateBottom"), map.get("createDateTop"));

        List<User> userList = DAO.findListByColumns(columns);
        log.info(userList.toString());
        return userList;
    }
}